/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controladores;

import Controladores.exceptions.IllegalOrphanException;
import Controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidades.Carrera;
import Entidades.Facultad;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Miguel
 */
public class FacultadJpaController implements Serializable {

    public FacultadJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Facultad facultad) {
        if (facultad.getCarreraCollection() == null) {
            facultad.setCarreraCollection(new ArrayList<Carrera>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Carrera> attachedCarreraCollection = new ArrayList<Carrera>();
            for (Carrera carreraCollectionCarreraToAttach : facultad.getCarreraCollection()) {
                carreraCollectionCarreraToAttach = em.getReference(carreraCollectionCarreraToAttach.getClass(), carreraCollectionCarreraToAttach.getIdCarrera());
                attachedCarreraCollection.add(carreraCollectionCarreraToAttach);
            }
            facultad.setCarreraCollection(attachedCarreraCollection);
            em.persist(facultad);
            for (Carrera carreraCollectionCarrera : facultad.getCarreraCollection()) {
                Facultad oldFacultadidFacultadOfCarreraCollectionCarrera = carreraCollectionCarrera.getFacultadidFacultad();
                carreraCollectionCarrera.setFacultadidFacultad(facultad);
                carreraCollectionCarrera = em.merge(carreraCollectionCarrera);
                if (oldFacultadidFacultadOfCarreraCollectionCarrera != null) {
                    oldFacultadidFacultadOfCarreraCollectionCarrera.getCarreraCollection().remove(carreraCollectionCarrera);
                    oldFacultadidFacultadOfCarreraCollectionCarrera = em.merge(oldFacultadidFacultadOfCarreraCollectionCarrera);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Facultad facultad) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Facultad persistentFacultad = em.find(Facultad.class, facultad.getIdFacultad());
            Collection<Carrera> carreraCollectionOld = persistentFacultad.getCarreraCollection();
            Collection<Carrera> carreraCollectionNew = facultad.getCarreraCollection();
            List<String> illegalOrphanMessages = null;
            for (Carrera carreraCollectionOldCarrera : carreraCollectionOld) {
                if (!carreraCollectionNew.contains(carreraCollectionOldCarrera)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Carrera " + carreraCollectionOldCarrera + " since its facultadidFacultad field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Carrera> attachedCarreraCollectionNew = new ArrayList<Carrera>();
            for (Carrera carreraCollectionNewCarreraToAttach : carreraCollectionNew) {
                carreraCollectionNewCarreraToAttach = em.getReference(carreraCollectionNewCarreraToAttach.getClass(), carreraCollectionNewCarreraToAttach.getIdCarrera());
                attachedCarreraCollectionNew.add(carreraCollectionNewCarreraToAttach);
            }
            carreraCollectionNew = attachedCarreraCollectionNew;
            facultad.setCarreraCollection(carreraCollectionNew);
            facultad = em.merge(facultad);
            for (Carrera carreraCollectionNewCarrera : carreraCollectionNew) {
                if (!carreraCollectionOld.contains(carreraCollectionNewCarrera)) {
                    Facultad oldFacultadidFacultadOfCarreraCollectionNewCarrera = carreraCollectionNewCarrera.getFacultadidFacultad();
                    carreraCollectionNewCarrera.setFacultadidFacultad(facultad);
                    carreraCollectionNewCarrera = em.merge(carreraCollectionNewCarrera);
                    if (oldFacultadidFacultadOfCarreraCollectionNewCarrera != null && !oldFacultadidFacultadOfCarreraCollectionNewCarrera.equals(facultad)) {
                        oldFacultadidFacultadOfCarreraCollectionNewCarrera.getCarreraCollection().remove(carreraCollectionNewCarrera);
                        oldFacultadidFacultadOfCarreraCollectionNewCarrera = em.merge(oldFacultadidFacultadOfCarreraCollectionNewCarrera);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = facultad.getIdFacultad();
                if (findFacultad(id) == null) {
                    throw new NonexistentEntityException("The facultad with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Facultad facultad;
            try {
                facultad = em.getReference(Facultad.class, id);
                facultad.getIdFacultad();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The facultad with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Carrera> carreraCollectionOrphanCheck = facultad.getCarreraCollection();
            for (Carrera carreraCollectionOrphanCheckCarrera : carreraCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Facultad (" + facultad + ") cannot be destroyed since the Carrera " + carreraCollectionOrphanCheckCarrera + " in its carreraCollection field has a non-nullable facultadidFacultad field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(facultad);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Facultad> findFacultadEntities() {
        return findFacultadEntities(true, -1, -1);
    }

    public List<Facultad> findFacultadEntities(int maxResults, int firstResult) {
        return findFacultadEntities(false, maxResults, firstResult);
    }

    private List<Facultad> findFacultadEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Facultad.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Facultad findFacultad(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Facultad.class, id);
        } finally {
            em.close();
        }
    }

    public int getFacultadCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Facultad> rt = cq.from(Facultad.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
